# coding:utf-8
'''
@Author:Hunter.Zhang
@Target: export Bmob data to Parse database - postgresql.
'''
import datetime
from bmob import *
import pdb
import psycopg2
import random

#Bmob
class ReadData:
    def __init__(self):
        self.b = Bmob("f5b3dbd027720a6f49c87b6a1a1c7a84", "616ee9149ae817bcd2f535b9a21050d4")

    def getTableRecords(self, tb_name, create_time):
        ret_str = str()
        if create_time is None:
            ret_str = self.b.find(
                tb_name,
                where=None,
                limit=300,
                skip=0,
                order='createdAt',
            ).stringData
        else:
            new_create_time = BmobDate((create_time[0] + datetime.timedelta(seconds=1)).strftime('%Y-%m-%d %H:%M:%S'))
            #pdb.set_trace()
            ret_str = self.b.find(
                tb_name,
                BmobQuerier().addWhereGreaterThan('createdAt', new_create_time),
                limit=300,
                skip=0,
                order='createdAt',
            ).stringData
        #pdb.set_trace()
        lst_all = json.loads(ret_str)['results']
        return lst_all

    def getTableRecordsByFilter(self, tb_name, filter_field, filter_value):
        ret_str = str()
        dct_where = dict()
        dct_where[filter_field] = filter_value
        ret_str = self.b.find(
            tb_name,
            where=dct_where,
            limit=300,
            skip=0,
            order='createdAt',
        ).stringData
        lst_all = json.loads(ret_str)['results']
        return lst_all


#Postgresql
class WriteData:
    def __init__(self):
        self.conn = psycopg2.connect(database='shiqu_parse', user='dbuser', password='meiyoumima', host='47.92.165.23',
                                     port='5432')
        self.cur = self.conn.cursor()

    def getLastCreatedTime(self, tb_name):
        # 从Parse表里面获取最后一条记录的时间。
        sql = 'select "createdAt" from "%s" where "flag" = 0 order by "%s" desc limit 1;' % (tb_name, 'createdAt')
        #print(sql)
        self.cur.execute(sql)
        result = self.cur.fetchone()
        print("最后记录创建时间:%s" % result)
        return result

    def setUserList(self, dct_record):
        #pdb.set_trace()
        objectId = dct_record['objectId']
        username = dct_record['username']
        alphabet = 'abcdefghijklmnopqrstuvwxyz0123456789'
        new_user_name = ''.join(random.sample(alphabet,16))
        mobile = str()
        if 'mobilePhoneNumber' in dct_record:
            mobile = dct_record['mobilePhoneNumber']
            username = new_user_name
        createdAt = dct_record['createdAt']
        updatedAt = dct_record['updatedAt']
        flag = 0
        authData = dict()
        if 'authData' in dct_record:
            authData = dct_record['authData']
            if authData:
                #pdb.set_trace()
                authData = json.dumps(dct_record['authData'])
        sql = '''insert into "_User" ("objectId", "username","phone", "authData","createdAt", "updatedAt", "flag") values ('%s', '%s', '%s', '%s', '%s', '%s','%s');''' % (
            objectId, username, mobile, authData, createdAt, updatedAt, flag)
        print(sql)
        try:
            self.cur.execute(sql)
        except Exception as e:
            print(e)
            self.conn.rollback()
            return False
        return True

    def setUserProfile(self, dct_record):
        #pdb.set_trace()
        objectId = dct_record['objectId']
        gender = 0
        if 'gender' in dct_record:
            gender = dct_record['gender']
        invite_code = str()
        if 'invite_code' in dct_record:
            invite_code = dct_record['invite_code']
        bg_img = str()
        if 'bgImgUrl' in dct_record:
            bg_img = dct_record['bgImgUrl']
        motto = str()
        if 'motto' in dct_record:
            motto = dct_record['motto']
        province = str()
        if 'province' in dct_record:
            province = dct_record['province']
        city = str()
        if 'city' in dct_record:
            city = dct_record['city']
        area = str()
        if 'area' in dct_record:
            area = dct_record['area']
        avatar = str()
        if 'avatarUrl' in dct_record:
            avatar = dct_record['avatarUrl']
        birthday = str()
        if 'birthday' in dct_record:
            birthday = dct_record['birthday']
        nick_name = str()
        if 'nickName' in dct_record:
            nick_name = dct_record['nickName']
        real_name = str()
        if 'realName' in dct_record:
            real_name = dct_record['realName']
        createdAt = dct_record['createdAt']
        updatedAt = dct_record['updatedAt']

        alphabet = 'abcdefghijklmnopqrstuvwxyz0123456789'
        new_obj_id = ''.join(random.sample(alphabet,10))
        flag = 0
        
        sql = '''insert into "UserProfile" ("objectId", "gender","invite_code", "bg_img","motto",
            "province", "city", "district", "avatar", "birthday", 
            "nick_name","real_name","user_id","flag","createdAt", "updatedAt") values (
            '%s', '%s', '%s', '%s', '%s', '%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s');''' % (
            new_obj_id, gender, invite_code, bg_img, motto,
            province, city, area, avatar, birthday,
            nick_name, real_name, objectId, flag, createdAt, updatedAt)
        print(sql)
        try:
            self.cur.execute(sql)
        except Exception as e:
            print(e)
            self.conn.rollback()
            return False
        return True

    def setBodyList(self, dct_record):
        objectId = dct_record['objectId']
        user_id = dct_record['user_id']['objectId']
        weight = dct_record['weight']
        height = dct_record['height']
        dateTime = dct_record['dateTime']
        createdAt = dct_record['createdAt']
        updatedAt = dct_record['updatedAt']
        flag = 0
        try:
            self.cur.execute(
                'insert into "Body" ("objectId", "user_id", "weight", "height", "measure_date", "createdAt", "updatedAt","flag") values(%s, %s, %s, %s, %s, %s, %s,%s)',
                (objectId, user_id, weight, height, dateTime, createdAt, updatedAt,flag))
        except Exception as e:
            print(e)
            self.conn.rollback()
            return False
        return True

    def setEducationList(self, dct_record):
        objectId = dct_record['objectId']
        user_id = dct_record['user_id']['objectId']
        eIndex = 0
        if 'eIndex' in dct_record:
            eIndex = int(dct_record['eIndex'])
        school_name = str()
        if 'school_name' in dct_record:
            school_name = dct_record['school_name']
        specialty = str()
        if 'specialty' in dct_record:
            specialty = dct_record['specialty']
        start_time = str()
        if 'start_time' in dct_record:
            start_time = dct_record['start_time']
        end_time = str()
        if 'end_time' in dct_record:
            end_time = dct_record['end_time']
        createdAt = dct_record['createdAt']
        updatedAt = dct_record['updatedAt']
        flag = 0
        try:
            self.cur.execute(
                'insert into "Education" ("objectId", "user_id", "e_index", "school_name", "major_name", "start_time", "end_time", "createdAt", "updatedAt","flag") values(%s, %s, %s, %s, %s, %s, %s, %s, %s,%s)',
                (objectId, user_id, eIndex, school_name, specialty, start_time, end_time, createdAt, updatedAt,flag))
        except Exception as e:
            print(e)
            self.conn.rollback()
            return False
        return True

    def setReadList(self, dct_record):
        objectId = dct_record['objectId']
        user_id = dct_record['user_id']['objectId']
        book_id = dct_record['book_id']['objectId']
        thought = dct_record['thought']
        createdAt = dct_record['createdAt']
        updatedAt = dct_record['updatedAt']
        flag = 0
        try:
            self.cur.execute(
                'insert into "Read" ("objectId", "user_id", "book_id", "impression", "createdAt", "updatedAt","flag") values(%s, %s, %s, %s, %s, %s,%s)',
                (objectId, user_id, book_id, thought, createdAt, updatedAt,flag))
        except Exception as e:
            print(e)
            self.conn.rollback()
            return False
        return True

    def setSkillList(self, dct_record):
        objectId = dct_record['objectId']
        user_id = dct_record['user_id']['objectId']
        interest_name = dct_record['interest_name']
        certificate = '无'
        if 'certificate' in dct_record:
            certificate = dct_record['certificate']
        start_time = dct_record['start_time']
        star_num = dct_record['star_num']
        createdAt = dct_record['createdAt']
        updatedAt = dct_record['updatedAt']
        flag = 0
        try:
            self.cur.execute(
                'insert into "Skill" ("objectId", "user_id", "name", "certificate", "level", "start_time",  "createdAt", "updatedAt","flag") values(%s, %s, %s, %s, %s, %s, %s, %s,%s)',
                (objectId, user_id, interest_name, certificate, star_num, start_time, createdAt, updatedAt,flag))
        except Exception as e:
            print(e)
            self.conn.rollback()
            return False
        return True

    def setJobList(self, dct_record):
        objectId = dct_record['objectId']
        user_id = dct_record['user_id']['objectId']
        company_name = dct_record['company_name']
        post_name = dct_record['post_name']
        work_desc = dct_record['work_desc']
        start_time = dct_record['start_time']
        end_time = dct_record['end_time']
        createdAt = dct_record['createdAt']
        updatedAt = dct_record['updatedAt']
        flag = 0
        try:
            self.cur.execute(
                'insert into "Job" ("objectId", "user_id", "company_name", "position", "desc", "start_time",  "end_time", "createdAt", "updatedAt","flag") values(%s, %s, %s, %s, %s, %s, %s, %s, %s,%s)',
                (objectId, user_id, company_name, post_name, work_desc, start_time, end_time, createdAt, updatedAt,flag))
        except Exception as e:
            print(e)
            self.conn.rollback()
            return False
        return True

    def setInterestList(self, dct_record):
        objectId = dct_record['objectId']
        user_id = dct_record['user_id']['objectId']
        interest_name = dct_record['interest_name']
        certificate = '无'
        if 'certificate' in dct_record:
            certificate = dct_record['certificate']
        start_time = dct_record['start_time']
        star_num = dct_record['star_num']
        createdAt = dct_record['createdAt']
        updatedAt = dct_record['updatedAt']
        flag = 0
        try:
            self.cur.execute(
                'insert into "Interest" ("objectId", "user_id", "name", "certificate", "level", "start_time",  "createdAt", "updatedAt","flag") values(%s, %s, %s, %s, %s, %s, %s, %s,%s)',
                (objectId, user_id, interest_name, certificate, star_num, start_time, createdAt, updatedAt,flag))
        except Exception as e:
            print(e)
            self.conn.rollback()
            return False
        return True

    def setSkillExperList(self, dct_record):
        objectId = dct_record['objectId']
        skill_id = dct_record['skill_id']['objectId']
        desc = dct_record['desc']
        createdAt = dct_record['createdAt']
        updatedAt = dct_record['updatedAt']
        flag = 0
        try:
            self.cur.execute(
                'insert into "SkillExper" ("objectId", "skill_id", "desc", "createdAt", "updatedAt","flag") values(%s, %s, %s, %s, %s,%s)',
                (objectId, skill_id, desc, createdAt, updatedAt,flag))
        except Exception as e:
            print(e)
            self.conn.rollback()
            return False
        return True

    def setInterestExperList(self, dct_record):
        objectId = dct_record['objectId']
        interest_id = dct_record['interest_id']['objectId']
        desc = dct_record['desc']
        createdAt = dct_record['createdAt']
        updatedAt = dct_record['updatedAt']
        flag = 0
        try:
            self.cur.execute(
                'insert into "InterestExper" ("objectId", "interest_id", "desc", "createdAt", "updatedAt","flag") values(%s, %s, %s, %s, %s,%s)',
                (objectId, interest_id, desc, createdAt, updatedAt,flag))
        except Exception as e:
            print(e)
            self.conn.rollback()
            return False
        return True

    def setMoneyList(self, dct_record):
        #print(dct_record)
        objectId = dct_record['objectId']
        user_id = dct_record['user_id']['objectId']
        quarter = dct_record['quarter']
        money = dct_record['money']
        type = dct_record['type']
        name = dct_record['name']
        #pdb.set_trace()
        rcd_date = dct_record['date']['iso']
        createdAt = dct_record['createdAt']
        updatedAt = dct_record['updatedAt']
        flag = 0
        try:
            self.cur.execute(
                'insert into "Money" ("objectId", "user_id", "quarter", "money", "type", "name", "date", "createdAt", "updatedAt","flag") values(%s, %s, %s, %s, %s, %s, %s, %s, %s,%s)',
                (objectId, user_id, quarter, money, type, name, rcd_date, createdAt, updatedAt,flag))
        except Exception as e:
            print(e)
            self.conn.rollback()
            return False
        return True

    def setTodoList(self, dct_record):
        #print(dct_record)
        objectId = dct_record['objectId']
        user_id = dct_record['user_id']['objectId']
        task_name = dct_record['task_name']
        task_time = dct_record['task_time']
        task_state = dct_record['task_state']
        task_desc = dct_record['task_desc']
        task_flag = dct_record['task_flag']
        task_repeat = dct_record['task_repeat']
        createdAt = dct_record['createdAt']
        updatedAt = dct_record['updatedAt']
        flag = 0
        try:
            self.cur.execute(
                'insert into "Todo" ("objectId", "user_id", "task_name", "task_time", "task_state", "task_desc", "task_flag", "task_repeat", "createdAt", "updatedAt","flag") values(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s,%s)',
                (objectId, user_id, task_name, task_time, task_state, task_desc, task_flag, task_repeat, createdAt, updatedAt,flag))
        except Exception as e:
            print(e)
            self.conn.rollback()
            return False
        return True

    def setDiaryList(self, dct_record):
        #print(dct_record)
        objectId = dct_record['objectId']
        user_id = dct_record['user_id']['objectId']
        weather = dct_record['weather']
        mood = dct_record['mood']
        date = dct_record['date']['iso']
        location = dct_record['location']
        question_id = str()
        if 'question_id' in dct_record:
            question_id = dct_record['question_id']['objectId']
        content_html = (dct_record['content_html'])
        content_data = json.dumps(dct_record['content_data'])
        createdAt = dct_record['createdAt']
        updatedAt = dct_record['updatedAt']
        #pdb.set_trace()
        flag = 0
        try:
            self.cur.execute(
                'insert into "Diary" ("objectId", "user_id", "weather", "mood", "date", "location", "question_id", "content_html", "content_data", "createdAt", "updatedAt","flag") values(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,%s)',
                (objectId, user_id, weather, mood, date, location, question_id,
                content_html, content_data,createdAt, updatedAt,flag))
        except Exception as e:
            print(e)
            self.conn.rollback()
            return False
        return True

    def setDiaryTemplateList(self, dct_record):
        #print(dct_record)
        objectId = dct_record['objectId']
        user_id = str()
        if 'user_id' in dct_record:
            user_id = dct_record['user_id']['objectId']
        questions = json.dumps(dct_record['questions'])
        type = dct_record['type']
        name = str()
        if 'name' in dct_record:
            name = dct_record['name']
        createdAt = dct_record['createdAt']
        updatedAt = dct_record['updatedAt']
        flag = 0
        try:
            self.cur.execute(
                'insert into "DiaryTemplate" ("objectId", "user_id", "questions", "type", "name", "createdAt", "updatedAt","flag") values(%s, %s, %s, %s, %s, %s, %s,%s)',
                (objectId, user_id, questions, type, name, createdAt, updatedAt,flag))
        except Exception as e:
            print(e)
            self.conn.rollback()
            return False
        return True

    def setDiaryQuestions(self, dct_record):
        #print(dct_record)
        objectId = dct_record['objectId']
        user_id = str()
        if 'user_id' in dct_record:
            user_id = dct_record['user_id']['objectId']
        question = dct_record['question']
        createdAt = dct_record['createdAt']
        updatedAt = dct_record['updatedAt']
        flag = 0
        try:
            self.cur.execute(
                'insert into "DiaryQuestions" ("objectId", "user_id", "question", "createdAt", "updatedAt","flag") values(%s, %s, %s, %s, %s,%s)',
                (objectId, user_id, question, createdAt, updatedAt,flag))
        except Exception as e:
            print(e)
            self.conn.rollback()
            return False
        return True

    def setIntegralTask(self, dct_record):
        #print(dct_record)
        objectId = dct_record['objectId']
        points = dct_record['task_bonus_point']
        task_type = dct_record['task_type']
        task_name = dct_record['task_name']
        createdAt = dct_record['createdAt']
        updatedAt = dct_record['updatedAt']
        flag = 0
        try:
            self.cur.execute(
                'insert into "IntegralTask" ("objectId", "score", "type", "name", "createdAt", "updatedAt","flag") values(%s, %s, %s, %s, %s, %s,%s)',
                (objectId, points, task_type, task_name, createdAt, updatedAt,flag))
        except Exception as e:
            print(e)
            self.conn.rollback()
            return False
        return True

    def setIntegralHistory(self, dct_record):
        print(dct_record)
        objectId = dct_record['objectId']
        user_id = str()
        if 'user_id' in dct_record:
            user_id = dct_record['user_id']['objectId']
        bonus_point = dct_record['bonus_point']
        task_name = dct_record['task_name']
        task_type = dct_record['task_type']
        createdAt = dct_record['createdAt']
        updatedAt = dct_record['updatedAt']
        flag = 0
        try:
            self.cur.execute(
                'insert into "IntegralHistory" ("objectId", "user_id", "score", "type", "name", "createdAt", "updatedAt","flag") values(%s, %s, %s, %s, %s, %s, %s,%s)',
                (objectId, user_id, bonus_point, task_type, task_name, createdAt, updatedAt,flag))
        except Exception as e:
            print(e)
            self.conn.rollback()
            return False
        return True

    def setIntegralExchange(self, dct_record):
        #print(dct_record)
        objectId = dct_record['objectId']
        user_id = dct_record['user_id']['objectId']
        task_id = dct_record['task_id']['objectId']
        task_type = dct_record['task_type']
        order_status = dct_record['order_status']
        exchange_num = dct_record['exchange_num']
        createdAt = dct_record['createdAt']
        updatedAt = dct_record['updatedAt']
        flag = 0
        try:
            self.cur.execute(
                'insert into "IntegralExchange" ("objectId", "user_id", "task_id", "order_status", "type", "exchange_num", "createdAt", "updatedAt","flag") values(%s, %s, %s, %s, %s, %s, %s, %s,%s)',
                (objectId, user_id, task_id, task_type, order_status, exchange_num, createdAt, updatedAt,flag))
        except Exception as e:
            print(e)
            self.conn.rollback()
            return False
        return True

    def setIntegralCommodity(self, dct_record):
        print(dct_record)
        objectId = dct_record['objectId']
        commodity_type = dct_record['commodity_type']
        commodity_picture = dct_record['commodity_picture']
        commodity_price = dct_record['commodity_price']
        commodity_name = dct_record['commodity_name']
        createdAt = dct_record['createdAt']
        updatedAt = dct_record['updatedAt']
        flag = 0
        try:
            self.cur.execute(
                'insert into "IntegralCommodity" ("objectId", "name", "price", "picture", "type", "createdAt", "updatedAt","flag") values(%s, %s, %s, %s, %s, %s, %s,%s)',
                (objectId, commodity_name, commodity_price, commodity_picture, commodity_type, createdAt, updatedAt,flag))
        except Exception as e:
            print(e)
            self.conn.rollback()
            return False
        return True

    def setIntegralCommodityCollect(self, dct_record):
        #print(dct_record)
        objectId = dct_record['objectId']
        user_id = dct_record['user_id']['objectId']
        commodity_id = dct_record['product_id']['objectId']
        createdAt = dct_record['createdAt']
        updatedAt = dct_record['updatedAt']
        flag = 0
        try:
            self.cur.execute(
                'insert into "IntegralCommodityCollect" ("objectId", "user_id", "commodity_id","createdAt", "updatedAt","flag") values(%s, %s, %s, %s, %s,%s)',
                (objectId, user_id, commodity_id, createdAt, updatedAt,flag))
        except Exception as e:
            print(e)
            self.conn.rollback()
            return False
        return True

    def setUserBlack(self, dct_record):
        #print(dct_record)
        objectId = dct_record['objectId']
        user_id = str()
        if 'curr_user' in dct_record:
            user_id = dct_record['curr_user']['objectId']
        black_user = dct_record['black_user']['objectId']
        createdAt = dct_record['createdAt']
        updatedAt = dct_record['updatedAt']
        flag = 0
        try:
            self.cur.execute(
                'insert into "UserBlack" ("objectId", "curr_user", "black_user","createdAt", "updatedAt","flag") values(%s, %s, %s, %s, %s,%s)',
                (objectId, user_id, black_user, createdAt, updatedAt,flag))
        except Exception as e:
            print(e)
            self.conn.rollback()
            return False
        return True

    def setPostBlackList(self, dct_record):
        #print(dct_record)
        objectId = dct_record['objectId']
        user_id = str()
        if 'black_user' in dct_record:
            user_id = dct_record['black_user']['objectId']
        dynamic_id = dct_record['dynamic_id']['objectId']
        createdAt = dct_record['createdAt']
        updatedAt = dct_record['updatedAt']
        flag = 0
        try:
            self.cur.execute(
                'insert into "PostBlackList" ("objectId", "black_user", "post_id","createdAt", "updatedAt","flag") values(%s, %s, %s, %s, %s,%s)',
                (objectId, user_id, dynamic_id, createdAt, updatedAt,flag))
        except Exception as e:
            print(e)
            self.conn.rollback()
            return False
        return True

    def setAddress(self, dct_record):
        #print(dct_record)
        objectId = dct_record['objectId']
        user_id = dct_record['user_id']['objectId']
        is_default = dct_record['is_default']
        name = dct_record['name']
        province = dct_record['province']
        city = dct_record['city']
        district = dct_record['district']
        exact_address = dct_record['exact_address']
        phone_num = dct_record['phone_num']
        createdAt = dct_record['createdAt']
        updatedAt = dct_record['updatedAt']
        flag = 0
        try:
            self.cur.execute(
                'insert into "Address" ("objectId", "user_id", "name","phone_num", "is_default", "province", "city", "district", "detail_address","createdAt", "updatedAt","flag") values(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,%s)',
                (objectId, user_id, name, phone_num, is_default, province, city, district, exact_address, createdAt, updatedAt,flag))
        except Exception as e:
            print(e)
            self.conn.rollback()
            return False
        return True

    def setHonor(self, dct_record):
        #print(dct_record)
        objectId = dct_record['objectId']
        user_id = dct_record['user_id']['objectId']
        type = dct_record['type']
        honor = dct_record['honor']
        createdAt = dct_record['createdAt']
        updatedAt = dct_record['updatedAt']
        flag = 0
        try:
            self.cur.execute(
                'insert into "Honor" ("objectId", "user_id", "type", "honor", "createdAt", "updatedAt","flag") values(%s, %s, %s, %s, %s, %s,%s)',
                (objectId, user_id, type, honor, createdAt, updatedAt,flag))
        except Exception as e:
            print(e)
            self.conn.rollback()
            return False
        return True

    def setScore(self, dct_record):
        #print(dct_record)
        objectId = dct_record['objectId']
        user_id = dct_record['user_id']['objectId']
        total = dct_record['total']
        increase = dct_record['increase']
        grow = dct_record['grow']
        interest = dct_record['interest']
        financial = dct_record['financial']
        health = dct_record['health']
        biz = dct_record['biz']
        createdAt = dct_record['createdAt']
        updatedAt = dct_record['updatedAt']
        flag = 0
        try:
            self.cur.execute(
                'insert into "Score" ("objectId", "user_id", "sum", "increase", "grow", "interest", "financial", "health", "biz", "createdAt", "updatedAt","flag") values(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,%s)',
                (objectId, user_id, total, increase, grow, interest, financial, health, biz, createdAt, updatedAt,flag))
        except Exception as e:
            print(e)
            self.conn.rollback()
            return False
        return True

    def setScoreHistory(self, dct_record):
        #print(dct_record)
        objectId = dct_record['objectId']
        user_id = dct_record['user_id']['objectId']
        action = int(dct_record['action'])
        target = dct_record['target']
        score = dct_record['score']
        createdAt = dct_record['createdAt']
        updatedAt = dct_record['updatedAt']
        flag = 0
        try:
            self.cur.execute(
                'insert into "ScoreHistory" ("objectId", "user_id", "action", "target", "score", "createdAt", "updatedAt","flag") values(%s, %s, %s, %s, %s, %s, %s,%s)',
                (objectId, user_id, action, target, score, createdAt, updatedAt,flag))
        except Exception as e:
            print(e)
            self.conn.rollback()
            return False
        return True

    def setTrail(self, dct_record):
        objectId =dct_record['objectId']
        spot_type = dct_record['type']
        footmark_id = dct_record['footmark_id']
        user_id = dct_record['user_id']['objectId']
        createdAt = dct_record['createdAt']
        updatedAt = dct_record['updatedAt']
        flag = 0
        sql = '''insert into "Trail" ("objectId", "user_id", "scenery_spot_id", "type", "createdAt", "updatedAt","flag") values ('%s', '%s', '%s', '%s', '%s', '%s','%s');''' % (
            objectId, user_id, footmark_id, spot_type, createdAt, updatedAt,flag)
        print(sql)
        try:
            self.cur.execute(sql)
        except Exception as e:
            print(e)
            self.conn.rollback()
            return False
        return True

    def setFoodHeat(self, dct_record):
        objectId = dct_record['objectId']
        user_id = dct_record['user_id']['objectId']
        quantity_heat = dct_record['quantity_heat']
        name = dct_record['name']
        food_type = dct_record['type']
        createdAt = dct_record['createdAt']
        updatedAt = dct_record['updatedAt']
        flag = 0
        sql = '''insert into "FoodHeat" ("objectId", "user_id", "type", "name","calorie","createdAt", "updatedAt","flag") values ('%s', '%s', '%s', '%s','%s','%s','%s','%s')''' % (
            objectId, user_id, food_type, name, quantity_heat, createdAt, updatedAt,flag)
        print(sql)
        try:
            self.cur.execute(sql)
        except Exception as e:
            print(e)
            self.conn.rollback()
            return False
        return True

    def setFollow(self, dct_record):
        obj_id = dct_record['objectId']
        user_id = dct_record['user_id']['objectId']
        follow_id = dct_record['follow_id']['objectId']
        createdAt = dct_record['createdAt']
        updatedAt = dct_record['updatedAt']
        flag = 0
        sql = '''insert into "Follow" ("objectId", "user_id", "follow_id", "createdAt", "updatedAt","flag") values ('%s','%s','%s','%s','%s','%s');''' % (
            obj_id, user_id, follow_id, createdAt, updatedAt,flag
        )
        print(sql)
        try:
            self.cur.execute(sql)
        except Exception as e:
            print(e)
            self.conn.rollback()
            return False
        return True

    def setFeedBack(self, dct):
        obj_id = dct['objectId']
        platform = dct['platform']
        version = dct['version']
        model = dct['model']
        if 'brand' not in dct:
            brand = str()
        else:
            brand = dct['brand']
        user_id = dct['user_id']['objectId']
        if 'list' not in dct:
            lst = str()
        else:
            lst = dct['list']
            if len(lst) > 0:
                lst = lst[0]
            else:
                lst = str()
        content = dct['content']
        createdAt = dct['createdAt']
        updatedAt = dct['updatedAt']
        flag = 0
        sql = '''insert into "Feedback" ("objectId", "user_id", "platform", "version", "model", "brand", "content", "pictures","createdAt", "updatedAt","flag") values ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s');''' % (
            obj_id, user_id, platform, version, model, brand, content, lst, createdAt, updatedAt, flag 
        )
        print(sql)
        try:
            self.cur.execute(sql)
        except Exception as e:
            print(e)
            self.conn.rollback()
            return False
        return True

    def setPlan(self, dct):
        obj_id = dct['objectId']
        #如果user_id有值的话，这条记录忽略
        #print(dct)
        if 'user_id' in dct:
            user_id = dct['user_id']['objectId']
            #pdb.set_trace()
            if user_id:
                return False
        if 'isCollect' in dct:
            is_collect = dct['isCollect']
        else:
            is_collect = False
        if 'collectCount' in dct:
            collect_count = dct['collectCount']
        else:
            collect_count = 0
        if 'pickCount' in dct:
            pick_count = dct['pickCount']
        else:
            pick_count = 0
        if 'isPublic' in dct:
            is_public = dct['isPublic']
        else:
            is_public = False
        plan_name = dct['plan_name']
        author_id = dct['author_id']['objectId']
        author_id = author_id.strip()
        item_title = dct['item_title']
        plan_duration = dct['plan_duration']
        if 'isSave' in dct:
            is_save = dct['isSave']
        else:
            is_save = False
        if 'plan_type_name' in dct:
            plan_type_name = dct['plan_type_name']
        else:
            plan_type_name = '兴趣'
        current_index = dct['current_index']
        plan_pic = dct['plan_pic']
        plan_tag = json.dumps(dct['plan_tag_i'])
        plan_progress = dct['plan_progress']
        plan_desc = dct['plan_synopsis']
        createdAt = dct['createdAt']
        updatedAt = dct['updatedAt']
        flag = 0
        sql = '''insert into "Plan" ("objectId", "name", "desc", "thumbnail", "tag", "type", "duration", "is_public", "pick_count", "collect_count", "author_id", "createdAt", "updatedAt","flag")
            values ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s');''' % (
            obj_id, plan_name, plan_desc, plan_pic, 
            plan_tag, plan_type_name, 
            plan_duration, is_public, pick_count, collect_count, author_id, 
            createdAt, updatedAt, flag)
        print(sql)
        try:
            self.cur.execute(sql)
        except Exception as e:
            print(e)
            self.conn.rollback()
            return False
        return True

    def setPost(self,dct):
        obj_id = dct['objectId']
        #print(dct)
        #status = dct['status']
        if 'zan_count' in dct:
            zan_count = dct['zan_count']
        else:
            zan_count = 0
        if 'transmit_count' in dct:
            transmit_count = dct['transmit_count']
        else:
            transmit_count = 0
        if 'comment_count' in dct:
            comment_count = dct['comment_count']
        else:
            comment_count = 0
        if 'works' in dct:
            works = dct['works']['objectId']
        else:
            works = str()
        data_content = str()
        if 'data_content' in dct:
            data_content = dct['data_content']
        #data_html = dct['data_html']
        source_dynamic_id = str()
        if 'source_dynamic_id' in dct:
            source_dynamic_id = dct['source_dynamic_id'].strip()
        if 'zanUsers' in dct:
            zanUsers = json.dumps(dct['zanUsers'])
        else:
            zanUsers = str()
        #location = dct['location']
        location_area = str()
        if 'location_area' in dct:
            location_area = dct['location_area']
        location_latitude = 0
        if 'location_latitude' in dct:
            location_latitude = dct['location_latitude']
        location_longitude = 0
        if 'location_longitude' in dct:
            location_longitude = dct['location_longitude']
        location_name = str()
        if 'location_name' in dct:
            location_name = dct['location_name']
        owner_id = dct['owner_id']['objectId'].strip()
        #is_transmited = dct['isTransmited']
        is_transmited = False
        is_public = False
        if 'isPublic' in dct: 
            is_public = dct['isPublic']
        is_star = False
        if 'isStar' in dct:
            is_star = dct['isStar']
        if 'plan_id' in dct:
            plan_id = dct['plan_id']['objectId'].strip()
        else:
            plan_id = str()
        data_list = list()
        if 'data_list' in dct:
            data_list = json.dumps(dct['data_list'])
        data_title = str()
        if 'data_title' in dct:
            data_title = dct['data_title']
        #id = dct['id']
        post_type = 0
        if 'type' in dct:
            post_type = dct['type']
        createdAt = dct['createdAt']
        updatedAt = dct['updatedAt']
        flag = 0
        sql = '''insert into "Post" 
            ("objectId", "type","title","content","media_list",
            "is_public", "is_important", "is_transmited","user_id", 
            "location_latitude", "location_longitude", "location_name", "location_area","plan_id", "book_id",
            "source_post_id", "view_count", "like_count","comment_count","createdAt", "updatedAt","flag") values (
            '%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s',
            '%s','%s','%s','%s','%s','%s','%s','%s','%s','%s');''' % (
                obj_id, post_type, data_title, data_content, data_list,
                is_public, is_star, is_transmited, owner_id,
                location_latitude, location_longitude, location_name, location_area, plan_id, works,
                source_dynamic_id, 0, zan_count, comment_count, createdAt, updatedAt,flag)
        print(sql)
        try:
            self.cur.execute(sql)
        except Exception as e:
            print(e)
            self.conn.rollback()
            return False
        return True

    def setPostComment(self, dct):
        obj_id = dct['objectId']
        #type_id = dct['type_id']
        #comment_type = dct['type']
        content = dct['content']
        source_id = str()
        if 'originator_id' in dct:
            source_id = dct['originator_id']['objectId'].strip()
        target_id = str()
        if 'target_id' in dct:
            target_id = dct['target_id']['objectId'].strip()
        dynamic_id = dct['dynamic_id']['objectId'].strip()
        createdAt = dct['createdAt']
        updatedAt = dct['updatedAt']
        flag = 0
        sql = '''insert into "PostComment" ("objectId", "source_id", "target_id", "post_id", "content", "createdAt", "updatedAt","flag") values (
            '%s','%s','%s','%s','%s','%s','%s','%s');''' % (
             obj_id, source_id, target_id, dynamic_id, content, createdAt, updatedAt,flag)
        print(sql)
        try:
            self.cur.execute(sql)
        except Exception as e:
            print(e)
            self.conn.rollback()
            return False
        return True

    def setPostLike(self,dct):
        obj_id = dct['objectId']
        #print(dct)
        owner_id = dct['owner_id']['objectId'].strip()
        createdAt = dct['createdAt']
        updatedAt = dct['updatedAt']
        flag = 0
        if 'zanUsers' not in dct:
            return False
        zanUsers = dct['zanUsers']
        if len(zanUsers) == 0:
            return False
        for user in zanUsers:
            alphabet = 'abcdefghijklmnopqrstuvwxyz0123456789'
            new_obj_id = ''.join(random.sample(alphabet,10))
            #pdb.set_trace()
            sql = '''insert into "PostLike" ("objectId", "user_id","post_id","createdAt","updatedAt","flag") values (
                '%s', '%s','%s','%s','%s','%s');''' % (new_obj_id, owner_id, obj_id, createdAt, updatedAt,flag)
            print(sql)
            try:
                self.cur.execute(sql)
            except  Exception as e:
                self.conn.rollback()
                print(e)
                return False
        return True

    def setMessage(self,dct):
        obj_id = dct['objectId']
        source_id = str()
        if 'original_user_id' in dct:
            source_id = dct['original_user_id']['objectId']
        target_id = str()
        if 'target_id' in dct:
            target_id = dct['target_id']['objectId']
        dynamic_id = str()
        if 'dynamic_id' in dct:
            dynamic_id = dct['dynamic_id']['objectId']
        content = str()
        if 'content' in dct:
            content = dct['content']
        service_type = 0
        if 'type' in dct:
            service_type = dct['type']
        is_read = False
        if 'readFlag' in dct:
            is_read = dct['readFlag']
        createdAt = dct['createdAt']
        updatedAt = dct['updatedAt']
        flag = 0
        sql = '''insert into "Message" ("objectId", "source_user_id", "target_user_id", "post_id", "content", "type","is_read","flag","createdAt", "updatedAt") values (
            '%s','%s','%s','%s','%s','%s','%s','%s','%s','%s');''' % (
            obj_id, source_id, target_id, dynamic_id, content, service_type, is_read, flag, createdAt, updatedAt)
        print(sql)
        try:
            self.cur.execute(sql)
        except Exception as e:
            print(e)
            self.conn.rollback()
            return False
        return True

    def setPlanItem(self, dct):
        obj_id = dct['objectId'].strip()
        if 'plan_id' not in dct:
            return False
        plan_id = dct['plan_id']['objectId'].strip()
        item_title = str()
        if 'item_title' in dct:
            item_title = dct['item_title']
        item_remarks = str()
        if 'item_remarks' in dct:
            item_remarks = dct['item_remarks']
        isFinish = False
        if 'isFinish' in dct:
            isFinish = dct['isFinish']
        time_temp = str()
        if 'item_time' in dct:
            time_temp = dct['item_time']
        if isFinish:
            time_finish = time_temp
            time_schedule = str()
        else:
            time_schedule = time_temp
            time_finish = str()
        flag = 0
        createdAt = dct['createdAt']
        updatedAt = dct['updatedAt']
        sql = '''insert into "PlanItem" ("objectId", "plan_id", "item_title", "item_desc", "item_schedule_time", "item_finish_time", "flag", "createdAt", "updatedAt") values (
            '%s','%s','%s','%s','%s','%s','%s','%s','%s'); ''' % (
            obj_id, plan_id, item_title, item_remarks, time_schedule, time_finish, 0, createdAt, updatedAt)
        print(sql)
        #pdb.set_trace()
        try:
            self.cur.execute(sql)
        except Exception as e:
            print(e)
            self.conn.rollback()
            return False
        return True

    def setPlanUseItem(self, use_id, dct):
        obj_id = dct['objectId'].strip()
        if 'plan_id' not in dct:
            return False
        plan_id = dct['plan_id']['objectId'].strip()
        item_title = str()
        if 'item_title' in dct:
            item_title = dct['item_title']
        item_remarks = str()
        if 'item_remarks' in dct:
            item_remarks = dct['item_remarks']
        isFinish = False
        if 'isFinish' in dct:
            isFinish = dct['isFinish']
        time_temp = str()
        if 'item_time' in dct:
            time_temp = dct['item_time']
        if isFinish:
            time_finish = time_temp
            time_schedule = str()
        else:
            time_schedule = time_temp
            time_finish = str()
        flag = 0
        createdAt = dct['createdAt']
        updatedAt = dct['updatedAt']
        sql = '''insert into "PlanUseItem" ("objectId", "plan_use_id", "item_title", "item_desc", "item_schedule_time", "item_finish_time", "is_finished", "flag", "createdAt", "updatedAt") values (
            '%s','%s','%s','%s','%s','%s','%s','%s','%s','%s'); ''' % (
            obj_id, use_id, item_title, item_remarks, time_schedule, time_finish, isFinish, flag, createdAt, updatedAt)
        print(sql)
        #pdb.set_trace()
        try:
            self.cur.execute(sql)
        except Exception as e:
            self.conn.rollback()
            print(e)
            return False
        return True

    def getPlanIds(self):
        # 查找Plan表的所有id
        sql = '''select "objectId" from "Plan" order by "createdAt";'''
        #print(sql)
        #pdb.set_trace()
        self.cur.execute(sql)
        plan_all_ids = self.cur.fetchall()

        # 查找PlanItem表的最后一条id
        sql = '''select "plan_id" from "PlanItem" where flag = 0 order by "createdAt" desc limit 1;'''
        self.cur.execute(sql)
        last_plan_item_id = self.cur.fetchone()
        if last_plan_item_id:
            # 获取最后一条id所在的位置
            #pdb.set_trace()
            index = plan_all_ids.index(last_plan_item_id) + 1
            # 截取出最新的几个Plan表的id
            lst_plan_id = plan_all_ids[index:len(plan_all_ids)]
            #pdb.set_trace()
            return lst_plan_id
        return plan_all_ids

    def getPlanUseIds(self):
        # 查找PlanUse表的所有id
        sql = '''select "objectId","plan_id" from "PlanUse" order by "createdAt";'''
        #print(sql)
        self.cur.execute(sql)
        plan_all_ids = self.cur.fetchall()
        #pdb.set_trace()

        # 查找PlanItem表的最后一条id
        sql = '''select "plan_use_id" from "PlanUseItem" where flag = 0 order by "createdAt" desc limit 1;'''
        self.cur.execute(sql)
        last_plan_item_id = self.cur.fetchone()
        if last_plan_item_id:
            # 获取最后一条id所在的位置
            #pdb.set_trace()
            #index = plan_all_ids.index(last_plan_item_id)
            index = 0
            for tp_use_item in plan_all_ids:
                itm_obj_id = tp_use_item[0]
                itm_plan_id = tp_use_item[1]
                if last_plan_item_id == itm_obj_id:
                    break
                index += 1 
            # 截取出最新的几个Plan表的id
            #pdb.set_trace()
            index += 1
            lst_plan_id = plan_all_ids[index:len(plan_all_ids)]
            return lst_plan_id
        return plan_all_ids

    def setPlanUse(self, dct):
        obj_id = dct['objectId']
        #如果user_id有值的话，这条记录忽略
        #print(dct)
        if 'user_id' in dct:
            user_id = dct['user_id']['objectId']
            #pdb.set_trace()
            if not user_id:
                return False
        else:
            return False
        if 'isCollect' in dct:
            is_collect = dct['isCollect']
        else:
            is_collect = False
        if 'collectCount' in dct:
            collect_count = dct['collectCount']
        else:
            collect_count = 0
        if 'pickCount' in dct:
            pick_count = dct['pickCount']
        else:
            pick_count = 0
        if 'isPublic' in dct:
            is_public = dct['isPublic']
        else:
            is_public = False
        plan_name = dct['plan_name']
        author_id = dct['author_id']['objectId']
        author_id = author_id.strip()
        item_title = dct['item_title']
        plan_duration = dct['plan_duration']
        if 'isSave' in dct:
            is_save = dct['isSave']
        else:
            is_save = False
        if 'plan_type_name' in dct:
            plan_type_name = dct['plan_type_name']
        else:
            plan_type_name = '兴趣'
        current_index = dct['current_index']
        plan_pic = dct['plan_pic']
        plan_tag = json.dumps(dct['plan_tag_i'])
        plan_progress = dct['plan_progress']
        plan_desc = dct['plan_synopsis']
        alphabet = 'abcdefghijklmnopqrstuvwxyz0123456789'
        new_obj_id = ''.join(random.sample(alphabet,10))
        createdAt = dct['createdAt']
        updatedAt = dct['updatedAt']
        flag = 0
        sql = '''insert into "PlanUse" ("objectId", "plan_id", "user_id", "progress", "next_step", "duration", "curr_index", "is_archived",  "createdAt", "updatedAt","flag")
            values ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s');''' % (
            new_obj_id, obj_id, user_id, plan_progress, item_title, 
            plan_duration, current_index, is_save, createdAt, updatedAt,flag)
        print(sql)
        try:
            self.cur.execute(sql)
        except Exception as e:
            print(e)
            self.conn.rollback()
            return False
        return True

    def setPlanCollect(self,dct):
        obj_id = dct['objectId'].strip()
        plan_id = str()
        if 'plan_id' in dct:
            plan_id = dct['plan_id']['objectId'].strip()
        user_id = str()
        if 'user_id' in dct:
            user_id = dct['user_id']['objectId'].strip()
        createdAt = dct['createdAt']
        updatedAt = dct['updatedAt']
        flag = 0
        sql = '''insert into "PlanCollect" ("objectId", "user_id", "plan_id", "createdAt", "updatedAt", "flag") values (
            '%s','%s','%s','%s','%s','%s');''' % (obj_id, user_id, plan_id, createdAt, updatedAt, flag)
        print(sql)
        try:
            self.cur.execute(sql)
        except Exception as e:
            print(e)
            self.conn.rollback()
            return False
        return True

    def closeConn(self):
        self.conn.commit()
        self.cur.close()
        self.conn.close()
        print("恭喜，Bmob->Postgresql数据单向同步完成！")


class SyncData:
    def __init__(self):
        self.bdata = ReadData()
        self.gdata = WriteData()

    def sync(self, source_tb_name, dest_tb_name):
        last_create_time = self.gdata.getLastCreatedTime(dest_tb_name)
        lst_data = self.bdata.getTableRecords(source_tb_name, last_create_time)
        #pdb.set_trace()
        index = 0
        b_ret = False
        for data in lst_data:
            if dest_tb_name == '_User':
                b_ret = self.gdata.setUserList(data)
            elif dest_tb_name == 'Body':
                b_ret = self.gdata.setBodyList(data)
            elif dest_tb_name == 'Education':
                b_ret = self.gdata.setEducationList(data)
            elif dest_tb_name == 'Read':
                b_ret = self.gdata.setReadList(data)
            elif dest_tb_name == 'Skill':
                b_ret = self.gdata.setSkillList(data)
            elif dest_tb_name == 'Job':
                b_ret = self.gdata.setJobList(data)
            elif dest_tb_name == 'Interest':
                b_ret = self.gdata.setInterestList(data)
            elif dest_tb_name == 'SkillExper':
                b_ret = self.gdata.setSkillExperList(data)
            elif dest_tb_name == 'InterestExper':
                b_ret = self.gdata.setInterestExperList(data)
            elif dest_tb_name == "Money":
                b_ret = self.gdata.setMoneyList(data)
            elif dest_tb_name == "Todo":
                b_ret = self.gdata.setTodoList(data)
            elif dest_tb_name == 'Diary':
                b_ret = self.gdata.setDiaryList(data)
            elif dest_tb_name == 'DiaryTemplate':
                b_ret = self.gdata.setDiaryTemplateList(data)
            elif dest_tb_name == 'DiaryQuestions':
                b_ret = self.gdata.setDiaryQuestions(data)
            elif dest_tb_name == "IntegralTask":
                b_ret = self.gdata.setIntegralTask(data)
            elif dest_tb_name == 'IntegralHistory':
                b_ret = self.gdata.setIntegralHistory(data)
            elif dest_tb_name == "IntegralExchange":
                b_ret = self.gdata.setIntegralExchange(data)
            elif dest_tb_name == 'IntegralCommodity':
                b_ret = self.gdata.setIntegralCommodity(data)
            elif dest_tb_name == 'IntegralCommodityCollect':
                b_ret = self.gdata.setIntegralCommodityCollect(data)
            elif dest_tb_name == 'UserBlack':
                b_ret = self.gdata.setUserBlack(data)
            elif dest_tb_name == 'PostBlackList':
                b_ret = self.gdata.setPostBlackList(data)
            elif dest_tb_name == 'Address':
                b_ret = self.gdata.setAddress(data)
            elif dest_tb_name == 'Honor':
                b_ret = self.gdata.setHonor(data)
            elif dest_tb_name == 'Score':
                b_ret = self.gdata.setScore(data)
            elif dest_tb_name == 'ScoreHistory':
                b_ret = self.gdata.setScoreHistory(data)
            elif dest_tb_name == 'Trail':
                b_ret = self.gdata.setTrail(data)
            elif dest_tb_name == 'FoodHeat':
                b_ret = self.gdata.setFoodHeat(data)
            elif dest_tb_name == 'Follow':
                b_ret = self.gdata.setFollow(data)
            elif dest_tb_name == 'Feedback':
                b_ret = self.gdata.setFeedBack(data)
            elif dest_tb_name == 'Plan':
                b_ret = self.gdata.setPlan(data)
            elif dest_tb_name == 'Post':
                b_ret = self.gdata.setPost(data)
            elif dest_tb_name == 'PostComment':
                b_ret = self.gdata.setPostComment(data)
            elif dest_tb_name == 'PostLike':
                b_ret = self.gdata.setPostLike(data)
            elif dest_tb_name == 'UserProfile':
                b_ret = self.gdata.setUserProfile(data)
            elif dest_tb_name == 'Message':
                b_ret = self.gdata.setMessage(data)
            elif dest_tb_name == 'PlanUse':
                b_ret = self.gdata.setPlanUse(data)
            elif dest_tb_name == 'PlanCollect':
                b_ret = self.gdata.setPlanCollect(data)
            if b_ret:
                index += 1
        print("%s表共插入:%d条记录" % (dest_tb_name, index))

    def syncPlanItem(self):
        plan_ids = self.gdata.getPlanIds()
        #pdb.set_trace()
        index = 0
        for pid in plan_ids:
            plan_id = pid[0]
            plan_items = self.bdata.getTableRecordsByFilter('t_plan_item', 'plan_id', plan_id)
            for item in plan_items:
                b_ret = self.gdata.setPlanItem(item)
                if b_ret:
                    index += 1
        print("共同步PlanItem:%d条." % index)

    def syncPlanUseItem(self):
        plan_ids = self.gdata.getPlanUseIds()
        #pdb.set_trace()
        index = 0
        for pid in plan_ids:
            use_id = pid[0]
            plan_id = pid[1]
            plan_items = self.bdata.getTableRecordsByFilter('t_plan_item', 'plan_id', plan_id)
            for item in plan_items:
                b_ret = self.gdata.setPlanUseItem(use_id, item)
                if b_ret:
                    index += 1
        print("共同步PlanUseItem:%d条." % index)


    def complete(self):
        self.gdata.closeConn()


if __name__ == '__main__':
    s = SyncData()

    print("+++++++++++++++++ 1、同步_User表开始+++++++++++++++++")
    s.sync('_User', '_User')
    print("\n\n")

    print("+++++++++++++++++ 2、同步Body表开始+++++++++++++++++")
    s.sync('t_health', 'Body')
    print("\n\n")

    print("+++++++++++++++++ 3、同步Education表开始+++++++++++++++++")
    s.sync('t_education', 'Education')
    print("\n\n")

    print("+++++++++++++++++ 4、同步Read表开始+++++++++++++++++")
    s.sync('t_readlist', 'Read')
    print("\n\n")

    print("+++++++++++++++++ 5、同步Skill表开始+++++++++++++++++")
    s.sync('t_skill', 'Skill')
    print("\n\n")

    print("+++++++++++++++++ 6、同步Job表开始+++++++++++++++++")
    s.sync('t_work', 'Job')
    print("\n\n")

    print("+++++++++++++++++ 7、同步Interest表开始+++++++++++++++++")
    s.sync('t_interest', 'Interest')
    print("\n\n")

    print("+++++++++++++++++ 8、同步SkillExper表开始+++++++++++++++++")
    s.sync('t_skill_exper', 'SkillExper')
    print("\n\n")

    print("+++++++++++++++++ 9、同步InterestExper表开始+++++++++++++++++")
    s.sync('t_interest_exper', 'InterestExper')
    print("\n\n")

    print("+++++++++++++++++ 10、同步Money表开始+++++++++++++++++")
    s.sync('t_income_expenditure', 'Money')
    print("\n\n")

    print("+++++++++++++++++ 11、同步Todo表开始+++++++++++++++++")
    s.sync('t_todo', 'Todo')
    print("\n\n")

    print("+++++++++++++++++ 12、同步Diary表开始+++++++++++++++++")
    s.sync('t_diary', 'Diary')
    print("\n\n")

    print("+++++++++++++++++ 13、同步DiaryTemplate表开始+++++++++++++++++")
    s.sync('t_diary_template', 'DiaryTemplate')
    print("\n\n")

    print("+++++++++++++++++ 14、同步DiaryQuestions表开始+++++++++++++++++")
    s.sync('t_diary_questions', 'DiaryQuestions')
    print("\n\n")

    print("+++++++++++++++++ 15、同步IntegralTask表开始+++++++++++++++++")
    s.sync('t_bonus_point_task', 'IntegralTask')
    print("\n\n")

    print("+++++++++++++++++ 16、同步IntegralHistory表开始+++++++++++++++++")
    s.sync('t_bonus_point_hst', 'IntegralHistory')
    print("\n\n")

    print("+++++++++++++++++ 17、同步IntegralExchange表开始+++++++++++++++++")
    s.sync('t_bonus_point_exchge', 'IntegralExchange')
    print("\n\n")

    print("+++++++++++++++++ 18、同步IntegralCommodity表开始+++++++++++++++++")
    s.sync('t_bonus_point_Cmmdty', 'IntegralCommodity')
    print("\n\n")

    print("+++++++++++++++++ 19、同步IntegralCommodityCollect表开始+++++++++++++++++")
    s.sync('t_commodity_collect', 'IntegralCommodityCollect')
    print("\n\n")

    print("+++++++++++++++++ 20、同步UserBlack表开始+++++++++++++++++")
    s.sync('t_black_user', 'UserBlack')
    print("\n\n")

    print("+++++++++++++++++ 21、同步PostBlackList表开始+++++++++++++++++")
    s.sync('t_dynamic_black_list', 'PostBlackList')
    print("\n\n")

    print("+++++++++++++++++ 22、同步Address表开始+++++++++++++++++")
    s.sync('t_address', 'Address')
    print("\n\n")

    print("+++++++++++++++++ 23、同步Honor表开始+++++++++++++++++")
    s.sync('t_honor', 'Honor')
    print("\n\n")

    print("+++++++++++++++++ 24、同步Score表开始+++++++++++++++++")
    s.sync('t_score', 'Score')
    print("\n\n")

    print("+++++++++++++++++ 25、同步ScoreHistory表开始+++++++++++++++++")
    s.sync('t_score_history', 'ScoreHistory')
    print("\n\n")

    print("+++++++++++++++++ 26、同步Trail表开始+++++++++++++++++")
    s.sync('t_footmark', 'Trail')
    print("\n\n")

    print("+++++++++++++++++ 27、同步FoodHeat表开始+++++++++++++++++")
    s.sync('t_food_heat_meter', 'FoodHeat')
    print("\n\n")

    print("+++++++++++++++++ 28、同步Follow表开始+++++++++++++++++")
    s.sync('t_follow', 'Follow')
    print("\n\n")

    print("+++++++++++++++++ 29、同步Feedback表开始+++++++++++++++++")
    s.sync('t_feedback', 'Feedback')
    print("\n\n")

    print("+++++++++++++++++ 30、同步Plan表开始+++++++++++++++++")
    s.sync('t_plan', 'Plan')
    print("\n\n")

    print("+++++++++++++++++ 31、同步Post表开始+++++++++++++++++")
    s.sync('t_dynamic', 'Post')
    print("\n\n")

    print("+++++++++++++++++ 32、同步PostComment表开始+++++++++++++++++")
    s.sync('t_comment', 'PostComment')
    print("\n\n")

    print("+++++++++++++++++ 33、同步PostLike表开始+++++++++++++++++")
    s.sync('t_dynamic', 'PostLike')
    print("\n\n")

    print("+++++++++++++++++ 34、同步UserProfile表开始+++++++++++++++++")
    s.sync('_User', 'UserProfile')
    print("\n\n")

    print("+++++++++++++++++ 35、同步Message表开始+++++++++++++++++")
    s.sync('t_message', 'Message')
    print("\n\n")

    print("+++++++++++++++++ 36、同步PlanItem表开始+++++++++++++++++")
    s.syncPlanItem()
    print("\n\n")

    print("+++++++++++++++++ 37、同步PlanUse表开始+++++++++++++++++")
    s.sync('t_plan', 'PlanUse')
    print("\n\n")

    print("+++++++++++++++++ 38、同步PlanCollect表开始+++++++++++++++++")
    s.sync('t_collect_plan', 'PlanCollect')
    print("\n\n")

    print("+++++++++++++++++ 39、同步PlanUseItem表开始+++++++++++++++++")
    s.syncPlanUseItem()
    print("\n\n\n")

    s.complete()
